package com.zhhh.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.wordnik.swagger.annotations.ApiOperation;
import com.zhhh.config.ResultStatus;
import com.zhhh.domain.BCompany;
import com.zhhh.domain.BOrganization;
import com.zhhh.domain.BPosition;
import com.zhhh.domain.BProject;
import com.zhhh.domain.BRole;
import com.zhhh.domain.BUser;
import com.zhhh.domain.BootstrapUiTreeNode;
import com.zhhh.domain.RolePowerTree;
import com.zhhh.model.ResultModel;
import com.zhhh.repository.BUserRepository;
import com.zhhh.repository.CompanyRepository;
import com.zhhh.repository.OrganizationRepository;
import com.zhhh.repository.PositionRepository;
import com.zhhh.repository.ProjectRepository;
import com.zhhh.repository.RoleRepository;

@RestController
@RequestMapping("/Organization")
public class OrganizationController {

	@Autowired
	private CompanyRepository cRepository;

	@Autowired
	private OrganizationRepository oRepository;

	@Autowired
	private PositionRepository pRepository;

	@Autowired
	private BUserRepository uRepository;

	@Autowired
	private ProjectRepository projectRepository;

	@Autowired
	private RoleRepository roleRepository;

	@RequestMapping(value = "/queryCompanyAll", method = RequestMethod.POST)
	@ApiOperation(value = "查询所有公司信息")
	public ResponseEntity<List<BCompany>> queryCompanyAll() {

		List<BCompany> findAll = (List<BCompany>) cRepository.findAll();
		return new ResponseEntity<>(findAll, HttpStatus.OK);
	}

	@RequestMapping(value = "/deleteCompany", method = RequestMethod.POST)
	@ApiOperation(value = "删除公司信息")
	public ResponseEntity<ResultModel> deleteCompany(@RequestParam Integer id) {
		cRepository.deleteById(id);
		List<BCompany> findAll = (List<BCompany>) cRepository.findAll();
		return new ResponseEntity<>(ResultModel.ok(findAll), HttpStatus.OK);
	}

	@RequestMapping(value = "/updateCompany", method = RequestMethod.POST)
	@ApiOperation(value = "增加或者修改公司信息")
	public ResponseEntity<ResultModel> updateCompany(@ModelAttribute("bCompany") BCompany bCompany) {
		BOrganization bOrganization;
		System.out.println(bCompany.getId());
		if(bCompany.getId()==null||bCompany.getId()==0) {
			bOrganization = new BOrganization();
		}else {
			System.out.println(bCompany.getCompCode());
			bOrganization = oRepository.findByOsysCode(bCompany.getCompCode());
			System.out.println(bOrganization);
		}
		
		bOrganization.setOname(bCompany.getCompName());
		bOrganization.setOcode(bCompany.getCompCode());
		bOrganization.setOsysCode(bCompany.getCompCode());
		bOrganization.setOdesc(bCompany.getCompDesc());
		bOrganization.setPid(0);
		bOrganization.setRemark(bCompany.getRemark());
		bOrganization.setManger(bCompany.getManger());
		oRepository.save(bOrganization);
		cRepository.save(bCompany);
		List<BCompany> findAll = (List<BCompany>) cRepository.findAll();
		return new ResponseEntity<>(ResultModel.ok(findAll), HttpStatus.OK);
	}

	@RequestMapping(value = "/queryOrganizationAll", method = RequestMethod.POST)
	@ApiOperation(value = "查询所有组织结构信息")
	public ResponseEntity<List<BOrganization>> queryOrganizationAll() {
		List<BOrganization> findAll = (List<BOrganization>) oRepository.findAll();
		return new ResponseEntity<>(findAll, HttpStatus.OK);
	}

	@RequestMapping(value = "/queryOrganizationAllJson", method = RequestMethod.POST)
	@ApiOperation(value = "查询所有组织结构信息JSON结构数据接口")
	public ResponseEntity<Object> queryOrganizationAllJson() {
		List<BOrganization> findAll = (List<BOrganization>) oRepository.findAll();
		List<BOrganization> rootList = BootstrapUiTreeNode.getRootNodeList(findAll);
		List<BootstrapUiTreeNode> list = new ArrayList<BootstrapUiTreeNode>();
		if (rootList.size() > 1) {

			for (int i = 0; i < rootList.size(); i++) {
				BootstrapUiTreeNode node = new BootstrapUiTreeNode();
				node.setId(rootList.get(i).getId() + "");
				node.setTitle(rootList.get(i).getOname());
				BootstrapUiTreeNode rootNode = BootstrapUiTreeNode.getChildNodeList2(findAll, node);
				list.add(rootNode);
			}
		} else if (rootList.size() == 1) {
			BootstrapUiTreeNode node = new BootstrapUiTreeNode();
			node.setId(rootList.get(0).getId() + "");
			node.setTitle(rootList.get(0).getOname());
			BootstrapUiTreeNode rootNode = BootstrapUiTreeNode.getChildNodeList2(findAll, node);
			list.add(rootNode);
		} else {
			System.out.println("没有找到ROOT节点");
		}
		return new ResponseEntity<>(list, HttpStatus.OK);
	}

	
	@RequestMapping(value = "/queryOrganizationUserAll", method = RequestMethod.POST)
	@ApiOperation(value = "查询一个组织结构下的人员集合")
	public ResponseEntity<BOrganization> queryOrganizationUserAll(@RequestParam Integer id) {
		BOrganization bOrganization = oRepository.findById(id).get();
		return new ResponseEntity<>(bOrganization, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/queryOrganizationUserAll2", method = RequestMethod.POST)
	@ApiOperation(value = "查询一个组织结构下的人员集合")
	public ResponseEntity<ResultModel> queryOrganizationUserAll2(@RequestParam Integer id) {
		BOrganization bOrganization = oRepository.findById(id).get();
		if(bOrganization!=null) {
			List<BUser> users = uRepository.findByUserList(bOrganization.getOsysCode());
			return new ResponseEntity<>(ResultModel.ok(users), HttpStatus.OK);
		}else{
			return new ResponseEntity<>(ResultModel.error(ResultStatus.DATA_DOES_NOT_EXIST), HttpStatus.SERVICE_UNAVAILABLE);
		}
		
	}
	
	@RequestMapping(value = "/queryOrganizationPositionAll", method = RequestMethod.POST)
	@ApiOperation(value = "查询一个组织结构下的岗位集合")
	public ResponseEntity<ResultModel> queryOrganizationPositionAll(@RequestParam Integer id) {
		BOrganization bOrganization = oRepository.findById(id).get();
		if(bOrganization!=null) {
			List<BPosition> positions = pRepository.findByUserList(bOrganization.getOsysCode());
			return new ResponseEntity<>(ResultModel.ok(positions), HttpStatus.OK);
		}else{
			return new ResponseEntity<>(ResultModel.error(ResultStatus.DATA_DOES_NOT_EXIST), HttpStatus.SERVICE_UNAVAILABLE);
		}
		
	}

	@RequestMapping(value = "/addOrganizationUser", method = RequestMethod.POST)
	@ApiOperation(value = "增加部门人员")
	public ResponseEntity<ResultModel> addOrganizationUser(@RequestParam Integer id,
			@RequestParam("jsonStr") String listJSON) {
		BOrganization bOrganization = oRepository.findById(id).get();
		List<BUser> users = JSON.parseArray(listJSON, BUser.class);
		for (int i = 0; i < users.size(); i++) {
			BUser user = users.get(i);
			user.setOSysCode(bOrganization.getOsysCode());;
			uRepository.save(user);
		}
		List<BUser> userResult = uRepository.findByUserList(bOrganization.getOsysCode());
		return new ResponseEntity<>(ResultModel.ok(userResult), HttpStatus.OK);
	}
	
	@RequestMapping(value = "/addOrganizationPosition", method = RequestMethod.POST)
	@ApiOperation(value = "增加部门岗位")
	public ResponseEntity<ResultModel> addOrganizationPosition(@RequestParam Integer id,
			@RequestParam("jsonStr") String listJSON) {
		BOrganization bOrganization = oRepository.findById(id).get();
		List<BPosition> positions = JSON.parseArray(listJSON, BPosition.class);
		for (int i = 0; i < positions.size(); i++) {
			BPosition position = positions.get(i);
			bOrganization.getPositions().add(position);
			position.getOrganizations().add(bOrganization);
			pRepository.save(position);
		}
		oRepository.save(bOrganization);
		/*
		 * 多对多 如果启用了双向关联 两种对象必须都进行保存一次，中间表才会新增数据建立关联关系
		 */

		BOrganization organizationResult = oRepository.findById(bOrganization.getId()).get();
		return new ResponseEntity<>(ResultModel.ok(organizationResult), HttpStatus.OK);
	}
	
	
	@RequestMapping(value = "/addOrganizationPosition2", method = RequestMethod.POST)
	@ApiOperation(value = "增加部门岗位")
	public ResponseEntity<ResultModel> addOrganizationPosition2(@RequestParam Integer id,
			@RequestParam("jsonStr") String listJSON) {
		BOrganization bOrganization = oRepository.findById(id).get();
		List<BPosition> positions = JSON.parseArray(listJSON, BPosition.class);
		for (int i = 0; i < positions.size(); i++) {
			BPosition position = positions.get(i);
			position.setOSysCode(bOrganization.getOsysCode());
			pRepository.save(position);
		}
		List<BPosition> positionResult = pRepository.findByUserList(bOrganization.getOsysCode());
		return new ResponseEntity<>(ResultModel.ok(positionResult), HttpStatus.OK);
	}
	
	
	@RequestMapping(value = "/deleteOrganizationPosition", method = RequestMethod.POST)
	@ApiOperation(value = "删除部门岗位")
	public ResponseEntity<ResultModel> deleteOrganizationPosition(@RequestParam Integer id,
			@RequestParam Integer pid) {
		BOrganization bOrganization = oRepository.findById(id).get();
		BPosition position = pRepository.findById(pid).get();
		bOrganization.getPositions().remove(position);
		position.getOrganizations().remove(bOrganization);
		pRepository.save(position);
		oRepository.save(bOrganization);
		/*
		 * 多对多 如果启用了双向关联 两种对象必须都进行保存一次，中间表才会新增数据建立关联关系
		 */
		BOrganization organizationResult = oRepository.findById(bOrganization.getId()).get();
		return new ResponseEntity<>(ResultModel.ok(organizationResult), HttpStatus.OK);
	}
	
	@RequestMapping(value = "/deleteOrganizationPosition2", method = RequestMethod.POST)
	@ApiOperation(value = "删除部门岗位")
	public ResponseEntity<ResultModel> deleteOrganizationPosition2(@RequestParam Integer id,
			@RequestParam Integer pid) {
		BOrganization bOrganization =  oRepository.findById(id).get();
		BPosition position = pRepository.findById(pid).get();
		position.setOSysCode("-1");
		pRepository.save(position);
		List<BPosition> positionResult = pRepository.findByUserList(bOrganization.getOsysCode());
		return new ResponseEntity<>(ResultModel.ok(positionResult), HttpStatus.OK);
	}
	
	
	
	
	@RequestMapping(value = "/deleteOrganizationUser", method = RequestMethod.POST)
	@ApiOperation(value = "删除部门人员")
	public ResponseEntity<ResultModel> deleteOrganizationUser(@RequestParam Integer id,
			@RequestParam Integer uid) {
		BOrganization bOrganization = oRepository.findById(id).get();
		BUser user =  uRepository.findById(uid).get();
		user.setOSysCode("-1");
		uRepository.save(user);
		
		List<BUser> userResult = uRepository.findByUserList(bOrganization.getOsysCode());
		return new ResponseEntity<>(ResultModel.ok(userResult), HttpStatus.OK);
	}
	

	@RequestMapping(value = "/deleteOrganization", method = RequestMethod.POST)
	@ApiOperation(value = "删除组织结构信息")
	public ResponseEntity<ResultModel> deleteOrganization(@RequestParam Integer id) {
		oRepository.deleteById(id);
		List<BOrganization> findAll = (List<BOrganization>) oRepository.findAll();
		return new ResponseEntity<>(ResultModel.ok(findAll), HttpStatus.OK);
	}

	@RequestMapping(value = "/updateOrganization", method = RequestMethod.POST)
	@ApiOperation(value = "增加或者修改结构信息")
	public ResponseEntity<ResultModel> updateOrganization(
			@ModelAttribute("bOrganization") BOrganization bOrganization) {
		oRepository.save(bOrganization);
		List<BOrganization> findAll = (List<BOrganization>) oRepository.findAll();
		return new ResponseEntity<>(ResultModel.ok(findAll), HttpStatus.OK);
	}

	@RequestMapping(value = "/queryPositionAll", method = RequestMethod.POST)
	@ApiOperation(value = "查询所有岗位信息")
	public ResponseEntity<List<BPosition>> queryPositionAll() {
		List<BPosition> findAll = (List<BPosition>) pRepository.findAll();
		return new ResponseEntity<>(findAll, HttpStatus.OK);
	}

	@RequestMapping(value = "/deletePosition", method = RequestMethod.POST)
	@ApiOperation(value = "删除岗位信息")
	public ResponseEntity<ResultModel> deletePosition(@RequestParam Integer id) {
		pRepository.deleteById(id);
		List<BPosition> findAll = (List<BPosition>) pRepository.findAll();
		return new ResponseEntity<>(ResultModel.ok(findAll), HttpStatus.OK);
	}

	@RequestMapping(value = "/updatePosition", method = RequestMethod.POST)
	@ApiOperation(value = "增加或者岗位信息")
	public ResponseEntity<ResultModel> updatePosition(@ModelAttribute("position") BPosition position) {
		pRepository.save(position);
		List<BPosition> findAll = (List<BPosition>) pRepository.findAll();
		return new ResponseEntity<>(ResultModel.ok(findAll), HttpStatus.OK);
	}

	@RequestMapping(value = "/queryUserAll", method = RequestMethod.POST)
	@ApiOperation(value = "查询所有用户信息")
	public ResponseEntity<List<BUser>> queryUserAll() {
		List<BUser> findAll = (List<BUser>) uRepository.findAll();
		return new ResponseEntity<>(findAll, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/queryNotDeptUserAll", method = RequestMethod.POST)
	@ApiOperation(value = "查询所有没有分配组织结构用户信息")
	public ResponseEntity<List<BUser>> queryNotDeptUserAll() {
		List<BUser> findAll = (List<BUser>) uRepository.queryNotDeptUserAll();
		return new ResponseEntity<>(findAll, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/queryNotDeptPositionAll", method = RequestMethod.POST)
	@ApiOperation(value = "查询所有没有分配组织结构岗位信息")
	public ResponseEntity<List<BPosition>> queryNotDeptPositionAll() {
		List<BPosition> positions = (List<BPosition>) pRepository.queryNotDeptUserAll();
		return new ResponseEntity<>(positions, HttpStatus.OK);
	}

	@RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
	@ApiOperation(value = "删除用户信息")
	public ResponseEntity<ResultModel> deleteUser(@RequestParam Integer id) {
		uRepository.deleteById(id);
		List<BUser> findAll = (List<BUser>) uRepository.findAll();
		return new ResponseEntity<>(ResultModel.ok(findAll), HttpStatus.OK);
	}

	@RequestMapping(value = "/updateUser", method = RequestMethod.POST)
	@ApiOperation(value = "增加或者用户信息")
	public ResponseEntity<ResultModel> updateUser(@ModelAttribute("user") BUser user) {
		uRepository.save(user);
		List<BUser> findAll = (List<BUser>) uRepository.findAll();
		return new ResponseEntity<>(ResultModel.ok(findAll), HttpStatus.OK);
	}

	@RequestMapping(value = "/queryProjectAll", method = RequestMethod.POST)
	@ApiOperation(value = "查询所有项目信息")
	public ResponseEntity<List<BProject>> queryProjectAll() {
		List<BProject> findAll = (List<BProject>) projectRepository.findAll();
		return new ResponseEntity<>(findAll, HttpStatus.OK);
	}

	@RequestMapping(value = "/deleteProject", method = RequestMethod.POST)
	@ApiOperation(value = "删除项目信息")
	public ResponseEntity<ResultModel> deleteProject(@RequestParam Integer id) {
		projectRepository.deleteById(id);
		List<BProject> findAll = (List<BProject>) projectRepository.findAll();
		return new ResponseEntity<>(ResultModel.ok(findAll), HttpStatus.OK);
	}

	@RequestMapping(value = "/updateProject", method = RequestMethod.POST)
	@ApiOperation(value = "增加或者修改项目信息")
	public ResponseEntity<ResultModel> updateProject(@ModelAttribute("project") BProject project) {
		projectRepository.save(project);
		List<BProject> findAll = (List<BProject>) projectRepository.findAll();
		return new ResponseEntity<>(ResultModel.ok(findAll), HttpStatus.OK);
	}

	@RequestMapping(value = "/queryRoleAll", method = RequestMethod.POST)
	@ApiOperation(value = "查询所有角色信息")
	public ResponseEntity<List<BRole>> queryRoleAll() {
		List<BRole> findAll = (List<BRole>) roleRepository.findAll();
		return new ResponseEntity<>(findAll, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/queryRoleAll2", method = RequestMethod.POST)
	@ApiOperation(value = "查询所有角色信息<角色权限维护，树形结构数据>")
	public ResponseEntity<List<BRole>> queryRoleAll2() {
		List<BRole> findAll = (List<BRole>) roleRepository.findAll();
		List<BRole> treeJson = RolePowerTree.getTreeJson(findAll);
		return new ResponseEntity<>(treeJson, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/queryRoleById", method = RequestMethod.POST)
	@ApiOperation(value = "根据ID查询角色信息")
	public ResponseEntity<ResultModel> queryRoleById(@RequestParam Integer id) {
		BRole role = roleRepository.findById(id).get();
		return new ResponseEntity<>(ResultModel.ok(role), HttpStatus.OK);
	}
	
	@RequestMapping(value = "/queryRoleById2", method = RequestMethod.POST)
	@ApiOperation(value = "根据ID查询角色信息")
	public ResponseEntity<List<RolePowerTree>> queryRoleById2(@RequestParam Integer id) {
		BRole role = roleRepository.findById(id).get();
		List<RolePowerTree> treeJson = RolePowerTree.getTreeJson(role);
		return new ResponseEntity<>(treeJson, HttpStatus.OK);
	}

	@RequestMapping(value = "/deleteRole", method = RequestMethod.POST)
	@ApiOperation(value = "删除角色信息")
	public ResponseEntity<ResultModel> deleteRole(@RequestParam Integer id) {
		roleRepository.deleteById(id);
		List<BRole> findAll = (List<BRole>) roleRepository.findAll();
		return new ResponseEntity<>(ResultModel.ok(findAll), HttpStatus.OK);
	}

	@RequestMapping(value = "/updateRole", method = RequestMethod.POST)
	@ApiOperation(value = "增加或者修改角色信息")
	public ResponseEntity<ResultModel> updateRole(@ModelAttribute("role") BRole role) {
		roleRepository.save(role);
		List<BRole> findAll = (List<BRole>) roleRepository.findAll();
		return new ResponseEntity<>(ResultModel.ok(findAll), HttpStatus.OK);
	}

}
